package com.taotao.manage.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.taotao.common.service.ApiService;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.pojo.ItemDesc;
@Service
public class ItemService extends BaseService<Item>{
	@Autowired
	private ItemDescService itemDescService;
	@Autowired
	private ApiService apiService;

	 // 从配置文件中读取后台服务器的地址
   @Value("${TAOTAO_WEB_URL}")
   private String TAOTAO_WEB_URL;
	public Boolean saveItem(Item item,String desc){
		//添加数据库中的id设置null 数据库中主键是自增
		item.setId(null);
		//添加状态为正常
		item.setStatus(1);
		//保存数据
		Boolean flag =super.save(item);
		if (flag) {
			
			ItemDesc itemDesc = new ItemDesc();
			itemDesc.setItemDesc(desc);
			itemDesc.setItemId(item.getId());
			//保存商品描述数据
			flag = this.itemDescService.save(itemDesc);
		}
		return flag;
	}

	

	public Boolean updateItem(Item item, String desc) {
	Boolean flag = super.updateByPrimaryKey(item);
	if(flag){
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemId(item.getId());
		itemDesc.setItemDesc(desc);
		flag = this.itemDescService.updateByPrimaryKey(itemDesc);
		try {
			//通知系统已经更新
			String url = TAOTAO_WEB_URL+"/item/cache/"+item.getId()+".html";
			this.apiService.doPost(url);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	return flag;
}
}
